#include "indcog.h"

#include <QPushButton>

IndCog::IndCog(QWidget *parent)
    : QWidget{parent}
{
    setAttribute(Qt::WA_StyledBackground, true);
    setStyleSheet("background-color: rgb(240, 240, 240)");
    setFixedSize(800, 376);
    m_right = new QLabel(this);
    m_right->setGeometry(width() / 2, 0, width() / 2, height());
    m_right->setStyleSheet("background-color: rgb(255, 255, 255)");
    SetLines();
    SetText();
}

void IndCog::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(this->width() / 4, this->height() / 2);
    DrawControlCircle(painter);
    DrawCoG(painter);
    DrawOutsideCircle(painter);
    DrawCoordinate(painter);
    DrawWarningCircle(painter);
}

void IndCog::DrawOutsideCircle(QPainter& painter)
{
    painter.save();

    QPen pen;
    pen.setWidth(1);
    pen.setColor(QColor(0xff808080));
    painter.setPen(pen);
    painter.drawEllipse(QPointF(0, 0), COORDINATE_RADIUS, COORDINATE_RADIUS);

    painter.restore();
}

void IndCog::DrawControlCircle(QPainter& painter)
{
    painter.save();

    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(0xff87ceeb));
    double radius = (double)m_controlLimit / 100 * COORDINATE_RADIUS;
    painter.drawEllipse(QPointF(m_CoG_x, m_CoG_y), radius, radius);

    painter.restore();
}

void IndCog::DrawWarningCircle(QPainter& painter)
{
    painter.save();

    QPen pen;
    pen.setWidth(2);
    pen.setColor(QColor(0xff003868));
    pen.setStyle(Qt::DashLine);
    painter.setPen(pen);
    double radius = (double)m_warningLimit / 100 * COORDINATE_RADIUS;
    painter.drawEllipse(QPointF(m_CoG_x, m_CoG_y), radius, radius);

    painter.restore();
}

void IndCog::DrawCoG(QPainter& painter)
{
    painter.save();

    painter.setBrush(m_CoG_color);
    QPen pen;
    pen.setWidth(1);
    pen.setColor(Qt::transparent);
    painter.setPen(pen);
    painter.drawEllipse(QPointF(m_CoG_x, m_CoG_y), COG_DIAMETER_RADIUS, COG_DIAMETER_RADIUS);

    painter.restore();
}

void IndCog::DrawCoordinate(QPainter& painter)
{
    painter.save();

    QPen pen;
    pen.setWidth(1);
    pen.setColor(QColor(0xff808080));
    painter.setPen(pen);

    QList<QLineF> lines;
    lines << QLineF(-COORDINATE_RADIUS, 0, COORDINATE_RADIUS, 0) << QLineF(0, -COORDINATE_RADIUS, 0, COORDINATE_RADIUS);
    painter.drawLines(lines);

    painter.restore();
}

void IndCog::SetCoGColor(QColor color)
{
    if (m_CoG_color != color) {
        m_CoG_color = color;
        update();
    }
}

void IndCog::SetLines()
{
    m_line1 = new QLabel(m_right);
    m_line1->setFrameShape(QLabel::HLine);
    m_line1->setGeometry(32, 260, 350, 1);
    QPalette palette = m_line1->palette();
    palette.setColor(QPalette::WindowText, QColor(0xff808080));
    m_line1->setPalette(palette);

    m_line2 = new QLabel(m_right);
    m_line2->setFrameShape(QLabel::HLine);
    m_line2->setGeometry(32, 300, 350, 1);
    m_line2->setPalette(palette);

    m_line3 = new QLabel(m_right);
    m_line3->setFrameShape(QLabel::HLine);
    m_line3->setGeometry(32, 340, 350, 1);
    m_line3->setPalette(palette);
}

void IndCog::SetText()
{
    QFont font;
    font.setFamily("Noto Sans");
    font.setPixelSize(18);

    m_warningText = new QLabel(m_right);
    m_warningText->setGeometry(32, 235, 150, 25);
    m_warningText->setText("Warning Limit");
    m_warningText->setAlignment(Qt::AlignBottom);
    m_warningText->setFont(font);
    m_warningText->setStyleSheet("color: #ff003868");

    m_warningValue = new QLabel(m_right);
    m_warningValue->setGeometry(340, 235, 50, 25);
    m_warningValue->setText(QString::fromStdString(std::to_string(m_warningLimit) + "%"));
    m_warningValue->setAlignment(Qt::AlignBottom);
    m_warningValue->setFont(font);
    m_warningValue->setStyleSheet("color: #ff003868");

    m_controlText = new QLabel(m_right);
    m_controlText->setGeometry(32, 275, 150, 25);
    m_controlText->setText("Control Limit");
    m_controlText->setAlignment(Qt::AlignBottom);
    m_controlText->setFont(font);
    m_controlText->setStyleSheet("color: #ff003868");

    m_controlValue = new QLabel(m_right);
    m_controlValue->setGeometry(340, 275, 50, 25);
    m_controlValue->setText(QString::fromStdString(std::to_string(m_controlLimit) + "%"));
    m_controlValue->setAlignment(Qt::AlignBottom);
    m_controlValue->setFont(font);
    m_controlValue->setStyleSheet("color: #ff003868");

    m_eccentricityText = new QLabel(m_right);
    m_eccentricityText->setGeometry(32, 315, 150, 25);
    m_eccentricityText->setText("Eccentricity");
    m_eccentricityText->setAlignment(Qt::AlignBottom);
    m_eccentricityText->setFont(font);
    m_eccentricityText->setStyleSheet("color: #ff003868");

    m_eccentricityValue = new QLabel(m_right);
    m_eccentricityValue->setGeometry(340, 315, 50, 25);
    m_eccentricityValue->setText(QString::fromStdString(std::to_string(m_eccentricity) + "%"));
    m_eccentricityValue->setAlignment(Qt::AlignBottom);
    m_eccentricityValue->setFont(font);
    m_eccentricityValue->setStyleSheet("color: #ff003868");
}
